package com.digitalart.siteBuilder
{
	import com.digitalart.commons.CustomEvent;
	import com.digitalart.commons.FontManager;
	import com.digitalart.siteBuilder.loader.Loader;
	import com.digitalart.siteBuilder.nav.NavController;
	import com.digitalart.siteBuilder.nav.Navigation;
	import com.digitalart.siteBuilder.section.Section;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.net.getClassByAlias;
	import flash.net.registerClassAlias;
	import flash.utils.getQualifiedClassName;
	
	[SWF (backgroundColor='#000000',  frameRate='40' ) ]
	public class MainController extends Sprite
	{
		
		private var _DEFAULT_SECTION_ALIAS:String;
		//public static const GALLERY_SECTION_ALIAS:String="com.digitalart.section.photogallery::GallerySection";
		//public static const PERFORMACE_SECTION_ALIAS:String="com.digitalart.section.performance::Performance";
		//public static const FEATURES_SECTION_ALIAS:String="com.digitalart.section.features::Features";
		
		private var _currentSection:Section; 
		private var _navigations:Array;
		protected var _loader:Loader;
		private static const FONT_MANAGER:FontManager = new FontManager();
		
		/**************************CONSTRUCTOR*************************************/		
		public function MainController(defaultAlias:String,ploader:Loader)
		{
			_loader = ploader;
			_DEFAULT_SECTION_ALIAS = defaultAlias;
			
			stage.scaleMode=StageScaleMode.NO_SCALE;
			stage.align=StageAlign.TOP_LEFT;
			registerSectionTypes();
			showLoader();
		}
	
		
		/**********************************************************************/
		
		/**********************************Methods*************************/
		private function  showLoader():void{
			loader.addEventListener(Loader.ON_LOAD_COMPLETE,onLoaderComplete);
			loader.load();
		}
		
		private function onLoaderComplete(e:Event):void{
			loader.show();	
			addChild(loader.getView());
			
			loadSeccion(DEFAULT_SECTION_ALIAS);
		}
		
		protected  function registerSectionTypes() : void {                             
			/*registerClassAlias(DEFAULT_SECTION_ALIAS,Index);*/
			/*registerClassAlias(GALLERY_SECTION_ALIAS,GallerySection);
			registerClassAlias(PERFORMACE_SECTION_ALIAS,Performance);
			registerClassAlias(FEATURES_SECTION_ALIAS,Features);     */               
		}
		
		private function addNav(nav:Navigation) : void {
			
			nav.addEventListener(Navigation.ON_LOAD_COMPLETE, onNavLoadComplete);
			nav.addEventListener(Navigation.CHANGE_SECTION_REQUEST, changeSection);
			
		}
		private function removeNav(index:int) : void {
			
			navigations[index].removeEventListener(Navigation.ON_LOAD_COMPLETE, onNavLoadComplete);
			navigations[index].removeEventListener(Navigation.CHANGE_SECTION_REQUEST, changeSection);
			navigations.splice(index,0);
		}
		
		private function onNavLoadComplete(event : Event) : void {
			var nav:Navigation = event.target as Navigation;
			nav.removeEventListener(Navigation.ON_LOAD_COMPLETE, onNavLoadComplete);
			this.addChild(nav.getView());
		}
		
		private function changeSection(event : CustomEvent) : void {
			loadSeccion(event.data);
		}
		
		private function loadSeccion(className : String) : void {
			trace(getQualifiedClassName(currentSection));
			trace(className);
			
			if( getQualifiedClassName(currentSection) != className ) {
				trace("CURRENT SECTION " + currentSection);
				var targetClass:Class = getClassByAlias(className) ;
				var newSurrentSection:Section = new targetClass();
				
				currentSection = newSurrentSection;
				currentSection.addEventListener(Section.ON_LOAD_COMPLETE, onSectionLoadComplete);
				currentSection.load();
			} 
		}
		
		private function onSectionLoadComplete(event : Event) : void {
			currentSection.removeEventListener(Section.ON_LOAD_COMPLETE, onSectionLoadComplete);
			
			if(this.numChildren > 0 ){
				this.removeChildAt(0);                  
			}
			this.addChildAt(currentSection,0);
			
			addNavigations();
			
		}
		
		private function addNavigations():void{
			trace("ADDING NAVS");
		}
		
		/**********************************************************************/
		/**************************Getters & Setters***************************/
		
		public function get currentSection() : Section {
			return _currentSection;
		}
		
		public function set currentSection(currentSection : Section) : void {
			_currentSection = currentSection;
		}
		public function get loader() : Loader {
			return _loader;
		}
		public function set loader(l:Loader) :void {
			_loader = l;
		}
		public function get navigations():Array{
			return _navigations;
		}
		public function get DEFAULT_SECTION_ALIAS ():String {
			return _DEFAULT_SECTION_ALIAS;
		} 
		/**********************************************************************/
	}
}